

//获取用户信息
var userInfo = localStorage.getItem('userInfo');
userInfo = JSON.parse(userInfo) || undefined;
if(userInfo==undefined && (window.location.pathname).indexOf('login.html')<0 
    && (window.location.pathname).indexOf('zhuce.html')<0){
    window.location.href = "login.html";
};
/**
 * 获取唯一标识
 */
function getUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
};
/**
 * hash算法
 */ 
var I64BIT_TABLE ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'.split('');
function hash(obj) {
    var hash = 5381;
    var i = obj.length - 1;

    if (typeof obj == 'string') {
        for (; i > -1; i--)
            hash += (hash << 5) + obj.charCodeAt(i);
    }else {
        for (; i > -1; i--)
            hash += (hash << 5) + obj[i];
    }
    var value = hash & 0x7FFFFFFF;

    var retValue = '';
    do {
        retValue += I64BIT_TABLE[value & 0x3F];
    }while (value >>= 6);

    return retValue;
}

/**
 * 动态加载JS
 * @param {string} url 脚本地址
 * @param {function} callback 回调函数
 */
function dynamicLoadJs(url, callback) {
    var body = document.getElementsByTagName('body')[0];
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = url;
    if (typeof (callback) == 'function') {
        script.onload = script.onreadystatechange = function () {
            if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
                callback();
                script.onload = script.onreadystatechange = null;
            }
        };
    }
    body.appendChild(script);
}
/**
 * 加载js
 * @param {*} url 
 */
function loadJs() {
    //js中有个变量arguments,可以访问所有传入的值
    for (var i = 0; i < arguments.length; i++) {
        dynamicLoadJs(arguments[i], null);
    }
}

/**
 * 登陆凭证
 */
var sign = hash(navigator.userAgent) + hash(navigator.appVersion) + hash(navigator.platform) 
         + hash(window.screen.width) + hash(window.screen.height) + hash(navigator.plugins);
var token = localStorage.getItem('token') || '';

/**
 * axios 配置
 */
axios.defaults.baseURL = baseURL; //1 将所有的主机名和端口 一起设置
axios.defaults.headers = { 'sign': sign, 'token': token } //设置headers
// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    if (response.status == 200) {
        var result = response.data;
        if (result.result == false && result.errcode == 403) {
            window.location.href = "login.html";
        }
        return result;
    } else {
        alert('请求错误');
        return response.data;
    }
}, function (error) {
    // 对响应错误做点什么
    // console.log(error);
    alert('服务不可用，请联系管理员！');
    return Promise.reject(error);
});

//处理返回参数
function interceptResponse(response){
    if (response.result == false && response.errcode == 403) {
        window.location.href = "login.html";
        return false;
    }
    return true;
}
//获取url参数
function GetQueryString(name) { 
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
    var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
    var context = ""; 
    if (r != null) 
       context = r[2]; 
    reg = null; 
    r = null; 
    return context == null || context == "" || context == "undefined" ? "" : context; 
}

//添加操作标识
function addDataOperation(key){
    var dataOperation = localStorage.getItem('dataOperation');
    if(dataOperation==null){
        dataOperation = {}; 
    }else{
        dataOperation = JSON.parse(dataOperation)
    };
    dataOperation[key] = true
    localStorage.setItem("dataOperation", JSON.stringify(dataOperation)); 
};
//获取操作标识
function getDataOperation(key){
    var dataOperation = localStorage.getItem('dataOperation');
    if(dataOperation!=null){
        dataOperation = JSON.parse(dataOperation)
        if(dataOperation[key]){
            delete dataOperation[key];
            localStorage.setItem("dataOperation", JSON.stringify(dataOperation)); 
            return true;
        }
    }
    return false;
};